package com.dd.shop4j.sys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.dd.shop4j.sys.mapper.SysConstantMapper;
import com.dd.shop4j.sys.model.SysConstant;
import com.dd.shop4j.sys.service.AddressService.AddressInfo;
import com.dd.shop4j.sys.service.AddressService.QueryType;

/**
 * 系统参数业务类
 * @author Administrator
 *
 */
@Service
@SuppressWarnings("all")
public class SysConstantService {
	
	private static final Logger logger = Logger.getLogger(SysConstantService.class);
	
	@Resource
	private SysConstantMapper sysConstantMapper;

	@Resource
	private AddressService addressService;
	
	private static Map<String, Object> constantMap;
	
	/**
	 * 初始化系统参数
	 */
	private void init() {
		if (constantMap == null) {
			constantMap = new HashMap<String, Object>();
		}
		if (constantMap.isEmpty()) {
			logger.debug("初始化系统参数");
			List<SysConstant> sysConstants = sysConstantMapper.queryBySelective(null);
			Assert.notEmpty(sysConstants);
			//初始化系统参数
			for (SysConstant sysConstant : sysConstants)
				constantMap.put(sysConstant.getCode(), sysConstant.getSysValue());
		}
	}

	/**
	 * 清空数据
	 */
	public void clear() {
		logger.debug("清空内存中的系统参数");
		constantMap.clear();
	}
	
	/**
	 * 重新加载数据
	 */
	public void reload() {
		logger.info("重新加载系统参数");
		clear();
		init();
	}
	
	/**
	 * 得到所有的省份信息
	 * @return
	 */
	public List<AddressInfo> getProvinces() {
		if (constantMap == null) {
			init();
		}
		if (constantMap.containsKey(QueryType.PROVINCE.toString())) {
			return (List<AddressInfo>) constantMap.get(QueryType.PROVINCE.toString());
		} else {
			List<AddressInfo> provinces = addressService.getAddress(QueryType.PROVINCE, 0);
			constantMap.put(QueryType.PROVINCE.toString(), provinces);
			return provinces;
		}
	}
	
	/**
	 * 得到省份下的所有城市
	 * @param provinceId 省份id
	 * @return
	 */
	public List<AddressInfo> getCitysByProvinceId(Integer provinceId) {
		if (constantMap == null) {
			init();
		}
		if (provinceId == null) provinceId = 0;
		if (constantMap.containsKey(QueryType.CITY.toString() + "_" + provinceId)) {
			return (List<AddressInfo>) constantMap.get(QueryType.CITY.toString() + "_" + provinceId);
		} else {
			List<AddressInfo> citys = addressService.getAddress(QueryType.CITY, provinceId);
			constantMap.put(QueryType.CITY.toString() + "_" + provinceId, citys);
			return citys;
		}
	}
	
	/**
	 * 得到城市下的所有区
	 * @param cityId 城市id
	 * @return
	 */
	public List<AddressInfo> getDistrictsByCityId(Integer cityId) {
		if (constantMap == null) {
			init();
		}
		if (cityId == null) cityId = 0;
		if (constantMap.containsKey(QueryType.DISTRICT.toString() + "_" + cityId)) {
			return (List<AddressInfo>) constantMap.get(QueryType.DISTRICT.toString() + "_" + cityId);
		} else {
			List<AddressInfo> districts = addressService.getAddress(QueryType.DISTRICT, cityId);
			constantMap.put(QueryType.DISTRICT.toString() + "_" + cityId, districts);
			return districts;
		}
	}
	
	/**
	 * 根据code得到系统参数
	 * @param code
	 * @return
	 */
	public String getString(String code) {
		if (constantMap == null) {
			init();
		}
		//断言constantMap一定不为空
		Assert.notEmpty(constantMap);
		logger.debug("获取系统参数:" + code);
		return (String) constantMap.get(code);
	}
	
	public String getString(String code, String defaultValue) {
		String value = getString(code);
		return value == null ? defaultValue : value;
	}
	
	/**
	 * 根据code得到系统参数
	 * @param code
	 * @return
	 */
	public int getInteger(String code) {
		try {
			return Integer.parseInt(getString(code));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public int getInteger(String code, int defaultValue) {
		String value = getString(code);
		return value == null ? defaultValue : Integer.parseInt(value);
	}
	
	/**
	 * 根据code得到系统参数
	 * @param code
	 * @return
	 */
	public double getDouble(String code) {
		return Double.parseDouble(getString(code));
	}
	
	public double getDouble(String code, double defaultValue) {
		String value = getString(code);
		return value == null ? defaultValue : Double.parseDouble(value);
	}
	
	/**
	 * 根据code得到系统参数
	 * @param code
	 * @return
	 */
	public float getFloat(String code) {
		return Float.parseFloat(getString(code));
	}
	
	public float getFloat(String code , float defaultValue) {
		String value = getString(code);
		return value == null ? defaultValue : Float.parseFloat(value);
	}
	
	/**
	 * 根据code得到系统参数
	 * @param code
	 * @return
	 */
	public boolean getBoolean(String code) {
		return Boolean.parseBoolean(getString(code));
	}
	
	public boolean getBoolean(String code, boolean defaultValue) {
		String value = getString(code);
		return value == null ? defaultValue : Boolean.parseBoolean(value);
	}
}
